const http  = require("./http");
export{}
module.exports={
    files (e:any) {
        let imgtype = e.currentTarget.dataset.imgtype;
        wx.chooseImage({
            count: 9,
            sizeType: ['original'],
            sourceType: ['album', 'camera'],
            success: (res)=> {
                console.log(res);
                let tempFilePaths = res.tempFilePaths;
                // let types = this.data[imgtype + "Arr"] ? this.data[imgtype + "Arr"] : [];
                let types = [];
                for (let i = 0; i < tempFilePaths.length; i++) {
                    if (types.indexOf(tempFilePaths[i]) == -1) {
                        types.push(tempFilePaths[i]);
                        this.upload(types[i], imgtype);
                    }
                }
                this.setData({
                    [imgtype + "Arr"]: types,
                    [imgtype]:[]
                });
            },
        });
    },
    /**
     * @titrle 单图上传
     */
   async file(e:any){
        let imgtype = e.currentTarget.dataset.imgtype;
        wx.chooseImage({
            count: 1,
            sizeType: ['original'],
            sourceType: ['album', 'camera'],
            success: (res) => {
                let tempFilePaths = res.tempFilePaths;
                // this.upload(tempFilePaths[0], imgtype);
                this.upup( imgtype,tempFilePaths[0])
            },
        });
    },

    async upup( imgtype:string,path:string){
        const url = await wx.$upFile({
            name: "file",
            path
        })
        this.setData({
            [imgtype]:url
        })
        console.log(url);
        
    },

    // 图片上传
    upload: function (filePath:string, imgtype:string) {
        let that = this;
        wx.uploadFile({
            url: "https://jiarong.xazbwl.com/v1/base/upload_file_to_qiniuyun",
            filePath: filePath,
            name: 'api',
            // name: 'file',
            header: {
                'content-type': 'multipart/form-data'
            },
            // 上传成功
            success:(res)=> {
                let imgArr = '' + JSON.parse(res.data).data;
                console.log(imgArr);
                let arr = this.data[imgtype] ? this.data[imgtype] : [];
                // let arr = [];
                arr.push(imgArr);
                that.setData({
                    [imgtype]: arr

                })
            }
        });
    },
    removeImg: function (e:any) {
        let that = this;
        wx.showModal({
            title: '删除提示',
            content: '是否要删除此图片',
            success: function (res) {
                if (res.confirm) {
                    let types = e.currentTarget.dataset.imgtype;
                    let src = e.currentTarget.dataset.imgsrc;
                    let arr = that.data[types];
                    let imgarr = that.data[types + "Arr"];
                    arr.splice(arr.indexOf(src), 1);
                    imgarr.splice(imgarr.indexOf(src), 1);
                    that.setData({
                        [types]: arr,
                        [types + "Arr"]: imgarr
                    })
                }
            }
        })
    },
}